<?php
require_once $_SERVER['DOCUMENT_ROOT']."/crc/geral/uteis/imports.php";
require_once $_SERVER['DOCUMENT_ROOT']."/crc/persistencia/dao/iHistoricoDao.php";

class HistoricoDao implements iHistoricoDao{

public function listarHistorico($idCasa, $limite){
	global $banco;
	require_once $_SERVER['DOCUMENT_ROOT']."/crc/persistencia/banco/conexao.php";				
	require_once $_SERVER['DOCUMENT_ROOT']."/crc/modelo/Historico.php";
		
	$sql = "SELECT data, descricao, status  
			FROM tb_historico
			WHERE id_casa = '$idCasa'
			ORDER BY data DESC LIMIT '$limite'";
	
	$rs = $banco->RodarQuery($sql);
		
	$historicos = array();
		
	while($linha = $banco->RetornaArray($rs)){
			$historico = new Historico();
			
			$historico->setData(utf8_decode($linha["data"]));
			$historico->setDescricao(utf8_decode($linha["descricao"]));
			$historico->setStatus(utf8_decode($linha["status"]));
						
			array_push($historicos, $historico);
	}	
	return $historicos;		
}

public function inserirHistorico(Historico $historico){
		global $banco;
		require_once $_SERVER['DOCUMENT_ROOT']."/crc/persistencia/banco/conexao.php";

		$descricao = $historico->getDescricao();
		$status = $historico->getStatus();
		$idCasa = $historico->getIdCasa();
	
		$sql = "INSERT INTO tb_historico(data, descricao, status, id_casa)
                VALUES ('now()', '$descricao', '$status', '$idCasa')";
								
		return $banco->ExecQuery(utf8_encode($sql));
	}
}
?>